import path from 'node:path'
// manifest.config.ts
import { defineManifestConfig } from '@uni-helper/vite-plugin-uni-manifest'
import { loadEnv } from 'vite'
import packageJson from './package.json'

function convertToVersionCode(version = '0.0.0') {
  if (!version || version === '0.0.0')
    return 0
  const parts = version.split('.')
  const length = parts.length
  let result = 0
  for (let i = 0; i < length; i++) {
    const currentPart = Number.parseInt(parts[i], 10)
    const multiplier = 10 ** (length - i - 1)
    result += (currentPart * multiplier)
  }
  return result
}

// 获取环境变量的范例
const env = loadEnv(process.env.NODE_ENV!, path.resolve(process.cwd(), 'env'))
// console.log('manifest.config.ts')
// console.log(env)
const {
  VITE_APP_TITLE,
  VITE_UNI_APPID,
  VITE_WX_APPID,
  VITE_APP_PUBLIC_BASE,
  VITE_FALLBACK_LOCALE,
} = env

const isDev = process.env.NODE_ENV === 'development'

export function appTitle() {
  return isDev ? `${VITE_APP_TITLE}[DEV]` : VITE_APP_TITLE
}

export default defineManifestConfig({
  name: appTitle(),
  appid: VITE_UNI_APPID,
  description: '',
  versionName: `${packageJson.version}`,
  versionCode: `${convertToVersionCode(packageJson.version)}`,
  transformPx: false,
  locale: VITE_FALLBACK_LOCALE, // 'zh-Hans'
  /* 5+App特有相关FT */
  'app-plus': {
    usingComponents: true,
    nvueStyleCompiler: 'uni-app',
    compilerVersion: 3,
    splashscreen: {
      alwaysShowBeforeRender: true,
      waiting: true,
      autoclose: true,
      delay: 0,
    },
    /* 模块配置 */
    modules: {},
    darkmode: true, // 开启支持深色模式
    themeLocation: 'theme.json', // 深色模式JSON文件，如果 theme.json 在根目录可省略
    /* 应用发布信息 */
    distribute: {
      /* android打包配置 */
      android: {
        minSdkVersion: 30,
        targetSdkVersion: 30,
        abiFilters: ['armeabi-v7a', 'arm64-v8a'],
        permissions: [
          '<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>',
          '<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>',
          '<uses-permission android:name="android.permission.VIBRATE"/>',
          '<uses-permission android:name="android.permission.READ_LOGS"/>',
          '<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>',
          '<uses-feature android:name="android.hardware.camera.autofocus"/>',
          '<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>',
          '<uses-permission android:name="android.permission.CAMERA"/>',
          '<uses-permission android:name="android.permission.GET_ACCOUNTS"/>',
          '<uses-permission android:name="android.permission.READ_PHONE_STATE"/>',
          '<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>',
          '<uses-permission android:name="android.permission.WAKE_LOCK"/>',
          '<uses-permission android:name="android.permission.FLASHLIGHT"/>',
          '<uses-feature android:name="android.hardware.camera"/>',
          '<uses-permission android:name="android.permission.WRITE_SETTINGS"/>',
        ],
      },
      /* ios打包配置 */
      ios: {
        dSYMs: false,
      },
      /* SDK配置 */
      sdkConfigs: {
        ad: {},
      },
      /* 图标配置 */
      icons: {
        android: {},
        ios: {},
      },
    },
  },
  /* 快应用特有相关 */
  quickapp: {},
  /* 小程序特有相关 */
  'mp-weixin': {
    appid: VITE_WX_APPID,
    setting: {
      condition: false, // 条件编译
      es6: true, // 将JS 编译成 ES5
      swc: false, // 使用SWC 编译脚本文件
      // @ts-ignore
      compileWorklet: false, // 使用worklet 代码
      urlCheck: false,
      postcss: true, // 上传代码的时样式自动补全
      uglifyFileName: false, // 代码保护功能
      minifyWXML: true, // 上传代码时自动压缩wxml文件
      minifyWXSS: true, // 上传代码时自动压缩样式文件
      minified: true, // 上传代码时自动压缩脚本文件
      ignoreUploadUnusedFiles: true, // 上传时过滤无依赖文件
      ignoreDevUnusedFiles: false,
    },
    usingComponents: true,
    mergeVirtualHostAttributes: true,
    // __usePrivacyCheck__: true,
    darkmode: true,
    themeLocation: 'theme.json',
    libVersion: 'latest', // 处理微信开发者工具报错

    /* 该配置，上传时会忽略已配置的静态资源文件 */
    packOptions: {
      ignore: [
        {
          type: 'folder',
          // 意思就是这里的图片不会上传到微信，可以作为以后服务器图片丢失后的替补
          value: 'static/fs-solid',
        },
      ],
    },

  },
  h5: {
    darkmode: true,
    themeLocation: 'theme.json',
    router: {
      base: VITE_APP_PUBLIC_BASE,
    },
  },
  'mp-alipay': {
    usingComponents: true,
  },
  'mp-baidu': {
    usingComponents: true,
  },
  'mp-toutiao': {
    usingComponents: true,
  },
  uniStatistics: {
    enable: false,
  },
  vueVersion: '3',
})
